/* 通过ES6面向对象对添加区要素方法封装 */
const style = {
  EndColor: 1,
  FillColor: 11,
  FillMode: 0,
  OutPenWidth: 1,
  OverMethod: 0,
  PatAngle: 1,
  PatColor: 1,
  PatHeight: 1,
  PatID: 11,
  PatWidth: 1,
};

/**
 *
 * @param {Array} position
 * @param {Array} attr [{key:"name",value:"武汉",type:"string"}]
 * @param {object} service {name,layerId}
 * @param {String} layer  调用中地接口,加载的地图文档图层
 */

class Area {
  static add({ position, attr, service, layer }) {
    /* 1、几何信息 */
    var pointObj = [];
    for (let item of position) {
      pointObj.push(new Zondy.Object.Point2D(item[0], item[1]));
    }
    //设置区要素的几何信息
    var gArc = new Zondy.Object.Arc(pointObj);
    //构成区要素折线
    var gAnyLine = new Zondy.Object.AnyLine([gArc]);
    //构成区要素
    var gRegion = new Zondy.Object.GRegion([gAnyLine]);
    //构成区要素的几何信息
    var fGeom = new Zondy.Object.FeatureGeometry({
      RegGeom: [gRegion],
    });
    /* 2、图形参数信息 */
    //设置区要素的图形参数信息
    var cRegionInfo = new Zondy.Object.CRegionInfo({ style });
    //要素图形参数信息
    var graphicInfo = new Zondy.Object.WebGraphicsInfo({
      InfoType: 3,
      RegInfo: cRegionInfo,
    });
    /* 3、属性 */
    //设置区要素的属性信息
    var attValue = attr.map((item) => item.value);
    /* 4、(几何+图形+属性) 构建要素*/
    //创建一个新的区要素
    var newFeature = new Zondy.Object.Feature({
      AttValue: attValue,
      fGeom: fGeom,
      GraphicInfo: graphicInfo,
    });
    newFeature.setFType(3);
    /* 5、构建要素集 */
    //创建一个要素数据集
    var featureSet = new Zondy.Object.FeatureSet();
    var fldNumber = attr.length;
    var fldType = attr.map((item) => item.type);
    var fldName = attr.map((item) => item.key);
    var cAttValue = new Zondy.Object.CAttStruct({
      FldNumber: fldNumber,
      FldType: fldType,
      FldName: fldName,
    });
    featureSet.AttStruct = cAttValue;
    featureSet.addFeature(newFeature);
    /* 6、调用服务,添加要素集 */
    //创建一个要素编辑服务对象
    var editDocFeature = new Zondy.Service.EditDocFeature(
      service.name,
      service.layerId,
      {
        ip: 'localhost',
        port: '6163', //访问IGServer的端口号，.net版为6163，Java版为8089
      }
    );
    editDocFeature.add(featureSet, this.onPloySuccess(layer));
  }
  static onPloySuccess(layer) {
    return function (result) {
      if (result) {
        alert('添加区要素成功！');
        layer.refresh();
      } else {
        alert('添加区要素失败！');
      }
    };
  }
}
